classdef TestDotsTheScreen < dotsTestCase
    
    properties
        theScreen;
    end
    
    methods
        function self = TestDotsTheScreen(name)
            self = self@dotsTestCase(name);
        end
        
        function setUp(self)
            self.setUp@dotsTestCase;
            
            dotsTheScreen.reset;
            self.theScreen = dotsTheScreen.theObject;
        end
        
        function tearDown(self)
            self.tearDown@dotsTestCase;
            dotsTheScreen.reset;
        end
        
        function testFrameAccounting(self)
            
            self.theScreen.open;
            
            % report increasing flip times
            n = 10;
            onset = zeros(1,n);
            number = zeros(1,n);
            swap = zeros(1,n);
            for ii = [1:n 1:n]
                [onset(ii), number(ii), swap(ii)] = ...
                    self.theScreen.nextFrame();
            end
            
            for ii = 2:n
                assertTrue(onset(ii) >= onset(ii-1), sprintf( ...
                    '%dth onset time %f should be greater than %f', ...
                    ii, onset(ii), onset(ii-1)));
                assertTrue(number(ii) >= number(ii-1), sprintf( ...
                    '%dth frame number %f should be greater than %f', ...
                    ii, number(ii), number(ii-1)));
                assertTrue(swap(ii) >= swap(ii-1), sprintf( ...
                    '%dth swap time %f should be greater than %f', ...
                    ii, swap(ii), swap(ii-1)));
            end
            
            self.theScreen.close;
        end
        
        function testRedundantOpenClose(self)
            self.theScreen.open;
            self.theScreen.open;
            self.theScreen.close;
            self.theScreen.open;
            self.theScreen.close;
            self.theScreen.close;
        end
        
        function testResetShouldCloseWindow(self)
            self.theScreen.open;
            dotsTheScreen.reset;
            assertTrue(self.theScreen.getDisplayNumber() < 0, ...
                'the screen should know its window has closed')
        end
    end
end